package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result add(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品参数：{}", dishDTO);
        dishService.save(dishDTO);
        // 清理缓存
        String pattern = "sky_dish_"+dishDTO.getCategoryId();
        clearCache(pattern);
        return Result.success();
    }

    /**
     * 清理redis缓存
     * @param pattern
     */
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 菜品分页
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页参数：{}", dishPageQueryDTO);
        PageResult page = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("批量删除菜品参数：{}", ids);
        dishService.deleteBatch(ids);
        // 清理缓存
        String pattern = "sky_dish_*";
        clearCache(pattern);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品参数：{}", id);
        DishVO dishVO = dishService.getVOById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品参数：{}", dishDTO);
        dishService.update(dishDTO);
        // 清理缓存
        String pattern = "sky_dish_*";
        clearCache(pattern);
        return Result.success();
    }

    /**
     * 菜品启售、停售
     *
     * @param status
     * @return
     */
    @ApiOperation("菜品启售、停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("id为{}的菜品启售、停售参数：{}", id, status);
        dishService.startOrStop(id, status);
        // 清理缓存
        String pattern = "sky_dish_*";
        clearCache(pattern);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> listByCategory(Long categoryId) {
        log.info("根据分类id查询菜品参数：{}", categoryId);
        // 构造参数
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .build();
        List<Dish> list = dishService.list(dish);
        return Result.success(list);
    }
}
